<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Validate;
use think\Session;
use app\admin\controller\Base;
use app\admin\controller\Index;

class Role extends Base
{
    public function index()
    {
    	$list = Db::name('role')
            ->field(['id', 'name','status','remark'])
            ->order('id', 'ASC')
            ->select();
       

       return view('/role/index', [
            'title' => '角色列表',
            'list' => $list
        ]);
    }

    public function add()
    {
        return view('role/add',[
            'title'=>'角色管理',
            'content'=>'添加角色'
            ]);
    }


    public function doadd()
    {
        $data = input('post.');
        
        $rule =['name'  => 'require|regex:/^[\x{4e00}-\x{9fa5}]{2,10}$/u',
                'status'   => 'require|boolean',
                'remark' => 'require|regex:/^[\x{4e00}-\x{9fa5}]{2,20}$/u'
                ];

        $message  =[
            'name.require' => '角色名必须填',
            'name.regex'     => '角色名2-10位中文汉字',
            'status.require'   => '状态必须填',
            'status.regex'=>'状态为启用或者禁用',
            'remark.require'  => '备注必须填',
            'remark.regex'=>'备注为2到20位中文'
            ];

        $validate = new Validate($rule,$message);
        $result   = $validate->check($data);


        if($result){
            
            $res =  db('role')->insertGetId($data);

            $this->success('新增成功', '/admin/Role');
        }else{
            $this->error('新增失败','/admin/Radd');
        }
    }


    public function nodelist($id='',$name='')
    {
         // var_dump($id,$name);die;
        $nodelist = Db::name('node')
            ->field(['id', 'name','mname','aname'])
            ->order('id', 'ASC')
            ->select();
        $node = Db::name('role_node')
            ->field('nid')
            ->where('rid='.$id)
            ->select();
        
        $arr = [];
        foreach($node as $k => $v){
            $arr[] =  $v['nid'];
        }
        // dump($arr);die;
        return view('role/nodelist',[
            'title'=>'给'.$name.'分配权限',
            'content'=>'权限列表',
            'nodelist'=>$nodelist,
            'arr'=>$arr,
            'rid'=>$id
            ]);
    }


    public function donode()
    {
        $rid = input('post.rid');
        $data = input('post.quanxian/a');
        
        foreach($data as $k => $v) {
            $datas[] = [
                'nid'=>$v,
                'rid'=>$rid,
            ]; 
        }

        
            Db::startTrans();
            try{
                Db::name('role_node')->where('rid',$rid)->delete();
                $result   =  db('role_node')->insertAll($datas);
                Db::commit();
                // $lastnode = action('Index/selectNode', ['id' => session('admin.id')], 'controller');
                // $lastnode = $this->selectNode(session('admin.id'));
                // $sess = Session::set('admin.node',$lastnode,'think');
                $this->success('权限更新成功', '/admin/Role');
            } catch (Exception $e) { 
                Db::rollback();
                $this->error('角色更新失败','/admin/Role');
            }
    }

    public function delete($id='')
    {
        Db::startTrans();
        try{
            Db::name('user_role')->where('rid',$id)->delete();
            Db::name('role_node')->where('rid',$id)->delete();
            Db::name('role')->where('id',$id)->delete();
            
            Db::commit();
            return 200;
        } catch (Exception $e) { 
            Db::rollback();
            return false;
        }  
    }

    public function update($id='')
    {
        $data = input('post.');
        Db::name('role')->where('id',$id)->update($data);
    }

}
